#!/usr/bin/python

import pymysql
import time
import struct
from timeit import default_timer as timer

db = pymysql.connect(host='localhost', port=3306,
                     user='root', passwd='', db='picsad', charset='utf8')

# 使用cursor()方法获取操作游标
cursor = db.cursor()

file_name = "/home/ubuntu/umm_000000_29.bin"
f = open(file_name, "rb")

li_arr = []
while True:
    x_str = f.read(4)
    if x_str == b'':
        break
    y_str = f.read(4)
    z_str = f.read(4)
    intensity_str = f.read(4)
    x = struct.unpack('f', x_str)[0]
    y = struct.unpack('f', y_str)[0]
    z = struct.unpack('f', z_str)[0]
    intensity = struct.unpack('f', intensity_str)[0]
    li_arr.append((x, y, z, intensity))

f.close()

print(len(li_arr))
start = timer()
cursor.executemany("INSERT INTO point_cloud(x_axis, y_axis, z_axis, intensity) values (%s, %s, %s, %s)", li_arr)
db.commit()
cursor.close()
db.close()
end = timer()
print(end - start)
